package com.qingcheng.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qingcheng.dao.AdminCommentMapper;
import com.qingcheng.dao.CommentMapper;
import com.qingcheng.entity.PageResult;
import com.qingcheng.pojo.comment.AdminComment;
import com.qingcheng.pojo.comment.Comment;
import com.qingcheng.service.comment.AdminCommentService;
import com.qingcheng.util.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class AdminCommentServiceImplHt implements AdminCommentService {

    @Autowired
    private AdminCommentMapper adminCommentMapper;

    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private IdWorker idWorker;

    /**
     * 返回全部记录
     *
     * @return
     */
    public List<AdminComment> findAll() {
        return adminCommentMapper.selectAll();
    }

    /**
     * 分页查询
     *
     * @param page 页码
     * @param size 每页记录数
     * @return 分页结果
     */
    public PageResult<AdminComment> findPage(int page, int size) {
        PageHelper.startPage(page, size);
        Page<AdminComment> adminComments = (Page<AdminComment>) adminCommentMapper.selectAll();
        return new PageResult<AdminComment>(adminComments.getTotal(), adminComments.getResult());
    }

    /**
     * 条件查询
     *
     * @param searchMap 查询条件
     * @return
     */
    public List<AdminComment> findList(Map<String, Object> searchMap) {
        Example example = createExample(searchMap);
        return adminCommentMapper.selectByExample(example);
    }

    /**
     * 分页+条件查询
     *
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    public PageResult<AdminComment> findPage(Map<String, Object> searchMap, int page, int size) {
        PageHelper.startPage(page, size);
        Example example = createExample(searchMap);
        Page<AdminComment> adminComments = (Page<AdminComment>) adminCommentMapper.selectByExample(example);
        return new PageResult<AdminComment>(adminComments.getTotal(), adminComments.getResult());
    }

    /**
     * 根据Id查询
     *
     * @param id
     * @return
     */
    public AdminComment findById(String id) {
        return adminCommentMapper.selectByPrimaryKey(id);
    }

    /**
     * 新增
     *
     * @param adminComment
     */
    public void add(AdminComment adminComment) {

        adminCommentMapper.insert(adminComment);
    }

    @Override
    public void save(AdminComment adminComment) {
        adminComment.setId(idWorker.nextId() + "");
        adminComment.setCommentId(adminComment.getCommentId());
        adminComment.setOperateTime(new Date());
        adminComment.setMessage(adminComment.getMessage());
        adminCommentMapper.insert(adminComment);
        Comment comment = commentMapper.selectByPrimaryKey(adminComment.getCommentId());
        comment.setResponse(adminComment.getMessage());
        comment.setResponseTime(new Date());
        commentMapper.updateByPrimaryKeySelective(comment);

    }

    /**
     * 修改
     *
     * @param adminComment
     */
    public void update(AdminComment adminComment) {
        adminCommentMapper.updateByPrimaryKeySelective(adminComment);
    }

    /**
     * 删除
     *
     * @param id
     */
    public void delete(String id) {
        adminCommentMapper.deleteByPrimaryKey(id);
    }

    /**
     * 构建查询条件
     *
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap) {
        Example example = new Example(AdminComment.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap != null) {
            // id
            if (searchMap.get("id") != null && !"".equals(searchMap.get("id"))) {
                criteria.andLike("id", "%" + searchMap.get("id") + "%");
            }
            // 操作人员
            if (searchMap.get("operator") != null && !"".equals(searchMap.get("operator"))) {
                criteria.andLike("operator", "%" + searchMap.get("operator") + "%");
            }
            // 回复内容
            if (searchMap.get("message") != null && !"".equals(searchMap.get("message"))) {
                criteria.andLike("message", "%" + searchMap.get("message") + "%");
            }
            // 评论ID
            if (searchMap.get("commentId") != null && !"".equals(searchMap.get("commentId"))) {
                criteria.andLike("commentId", "%" + searchMap.get("commentId") + "%");
            }


        }
        return example;
    }

}
